package com.tzu.ft.ui.comps;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/** 接收确认弹框 */
public class ReceiveDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	public static final int NO = 0;
	public static final int YES = 1;
	public static final int CONTINUE = 2;

	/** 确认信息 */
	public static String showConfirmInfo(JFrame frame, String title, String type) {
		ReceiveDialog dialog = new ReceiveDialog(frame, title, type);
		String retval = dialog.savepath;
		dialog.closeInstance();
		return retval;
	}

	private String name;
	private String type;

	private ReceiveDialog instence;

	public ReceiveDialog() {
		init(null);
	}

	public ReceiveDialog(JFrame frame, String name, String type) {
		super(frame);
		this.name = name;
		this.type = type;
		init(frame);
	}

	private JLabel label_filetype;
	private JLabel label_filename;

	private String savepath;

	private JFrame parentFrame;

	private void init(JFrame frame) {
		instence = this;
		parentFrame = frame;
		// 窗口大小
		setSize(500, 190);
		if (null != frame) {
			setLocation(frame.getX() + 30, frame.getY() + 30);
		}
		// 不可改变大小
		setResizable(false);
		// 设置弹出阻塞，父窗口将被锁定
		setModal(true);
		// 初始化
		setTitle("接收文件");
		getContentPane().setLayout(null);
		// 禁用关闭按钮
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 494, 161);
		getContentPane().add(panel);
		panel.setLayout(null);

		JButton btnNewButton = new JButton("接收");
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				fileChooser.setDialogTitle("保存文件");
				fileChooser.setSelectedFile(new File(name));//显示默认文件名
				int retval = fileChooser.showSaveDialog(parentFrame);
				// 确认选择
				if (retval == JFileChooser.APPROVE_OPTION) {
					File file = fileChooser.getSelectedFile();
					savepath = file.getAbsolutePath();
				}
				//dialog窗口阻塞控制
				setVisible(false);
			}
		});
		btnNewButton.setBounds(24, 121, 200, 30);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				savepath = null;
				setVisible(false);
			}
		});
		btnNewButton_1.setBounds(270, 121, 200, 30);
		panel.add(btnNewButton_1);

//		JButton btnNewButton_1_1 = new JButton("断点续传");
//		btnNewButton_1_1.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				retval = 2;
//				setVisible(false);
//			}
//		});
//		btnNewButton_1_1.setBounds(330, 121, 150, 30);
//		panel.add(btnNewButton_1_1);

		label_filetype = new JLabel("文件信息");
		label_filetype.setToolTipText("文件信息");
		label_filetype.setBounds(24, 81, 446, 30);
		panel.add(label_filetype);

		label_filename = new JLabel();
		label_filename.setLocation(10, 10);
		label_filename.setSize(470, 61);
		label_filename.setText("文件信息\r\n文件信息\r\n文件信息");
		label_filename.setToolTipText(label_filename.getText());

//		JScrollPane scrollPane = new JScrollPane(textArea_filename);
//		scrollPane.setBounds(10, 10, 471, 66);
//		scrollPane.setViewportView(textArea_filename);
		panel.add(label_filename);

		// 处理文件信息显示
		setFilename(name);
		setFiletype(type);

		// 显示窗口，dialog窗口阻塞控制
		setVisible(true);

	}

	public void setFilename(String name) {
		label_filename.setText(name);
	}

	public void setFiletype(String type) {
		label_filetype.setText(type);
	}

	public void closeInstance() {
		instence.dispose();
	}

}
